package com.example.androiddemo01.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.androiddemo01.R;
import com.example.androiddemo01.bean.Course;

import java.util.List;

public class CourseAdapter extends BaseAdapter {
    private List<Course> mList;
    private LayoutInflater mLayoutInflater;

    public CourseAdapter(List<Course> list, Context context) {
        mList = list;
        mLayoutInflater = LayoutInflater.from(context);
    }

    //刷新数据，传入新的数据列表
    public void updateData(List<Course> list) {
        mList.clear();
        mList = list;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Course getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        CourseAdapter.ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = mLayoutInflater.inflate(R.layout.course_item, null);
            viewHolder.idText = convertView.findViewById(R.id.course_id);
            viewHolder.nameText = convertView.findViewById(R.id.course_name);
            viewHolder.codeText = convertView.findViewById(R.id.course_code);
            viewHolder.teacherText = convertView.findViewById(R.id.teacher_name);
            viewHolder.ageText = convertView.findViewById(R.id.learning_hours);
            convertView.setTag(viewHolder);

        } else {
            viewHolder = (CourseAdapter.ViewHolder) convertView.getTag();
        }
        Course course = getItem(position);
        if (course != null) {
            convertView.setTag(R.integer.friend_tag, course);
            viewHolder.idText.setText(String.valueOf(course.getCourse_id()));
            viewHolder.nameText.setText(String.valueOf(course.getCourse_name()));
            viewHolder.codeText.setText(course.getCourse_code());
            viewHolder.teacherText.setText(course.getCourse_teacher());
            viewHolder.ageText.setText(course.getLearning_hours());
        }
        return convertView;
    }

    private static class ViewHolder {
        TextView nameText;
        TextView codeText;
        TextView teacherText;
        TextView ageText;
        TextView idText;
    }



}
